/***********************************************************************
 * Module:  AppFunctionAuth.java
 * Author:  Administrator
 * Purpose: Defines the Class AppFunctionAuth
 ***********************************************************************/

package com.vecspace.sso.entity;

import java.util.*;

/** 功能及授权类型(带children属性) */
public class AppFunctionAuth implements java.io.Serializable, Comparable<AppFunctionAuth>{

	private static final long serialVersionUID = 8257133250003143251L;
	private Long funcId;
	private AppFunction appFunction;	
	private int authType;

	private List<AppFunctionAuth> children;	//子功能
	
	public AppFunctionAuth(){
		children = new ArrayList<AppFunctionAuth>();
	}
	public AppFunctionAuth(long funcId, int authType){
		this.funcId = funcId;
		this.authType = authType;
		children = new ArrayList<AppFunctionAuth>();
	}

	public AppFunctionAuth(AppFunction appFunction,int authType) {
		this.appFunction = appFunction;
		this.authType = authType;
		if(appFunction != null){
			funcId = appFunction.getId();
		}
		children = new ArrayList<AppFunctionAuth>();
	}

	
	public String getFuncKey(){
		return String.valueOf(funcId);
	}
	
	public void setFuncKey(String funcKey){
		
	}
	
	public String getParentFuncKey(){
		if(appFunction == null)return "-1";
		Long funcId = appFunction.getId();
		
		return String.valueOf(funcId);
	}
	
	public void setParentFuncKey(String parentFuncKey){
		
	}

	public AppFunction getAppFunction() {
		return appFunction;
	}

	public void setAppFunction(AppFunction appFunction) {		
		this.appFunction = appFunction;
		if(appFunction != null){
			funcId = appFunction.getId();
		}
	}

	public int getAuthType() {
		return authType;
	}

	public void setAuthType(int authType) {
		this.authType = authType;
	}

	public List<AppFunctionAuth> getChildren() {
		return children;
	}

	public void setChildren(List<AppFunctionAuth> children) {
		this.children.clear();
		if (children != null) {
			this.children.addAll(children);
		}
	}

	/**添加子节点，并进行排序
	 * 
	 * @param child 
	 * @author: wanghua
	 */
	public void addChild(AppFunctionAuth child){
		if(children.size() ==0){
			children.add(child);
		}else{
			boolean add = false;
			int len = children.size();
			for(int i=0;i<len;++i){ //排序
				AppFunctionAuth item = children.get(i);
				if(item.compareTo(child) >0){
					children.add(i, child);
					add = true;
					break;
				}
			}
			if(!add){
				children.add(child);
			}
		}
	}
	
	public Long getFuncId() {
		return funcId;
	}

	public void setFuncId(Long funcId) {
		this.funcId = funcId;
	} 
	
	private String authType_Caption;
	public String getAuthType_Caption() {
		if(authType_Caption==null) {
			if(this.authType==1) {
				return "可访问";
			}else if(this.authType==2) {
				return "读写";
			}
		}
		return authType_Caption;
	}

	public void setAuthType_Caption(String value) {
		this.authType_Caption = value;
	}

	
	public int compareTo(AppFunctionAuth o) {
		if(o == null) return 1;
		if(o.appFunction == null)return 1;
		if(this.appFunction == null) return -1;
		
		if(o.appFunction.getOrdnum() == null)return 1;
		if(this.appFunction.getOrdnum() == null)return -1;
		
		return this.appFunction.getOrdnum().compareTo(o.appFunction.getOrdnum());
	}
}
